import { Button, Tabs, View, Text, List, WhiteSpace, InputItem, Icon, Toast } from 'antd-mobile';
import React, { Component } from 'react';
import { AppRegistry,  ScrollView } from 'react-native';
import { NavigationActions } from 'react-navigation'
import { Urls } from '../../utils/Urls';

const loginAction = NavigationActions.reset({
  index: 0,
  actions: [
    NavigationActions.navigate({ routeName: 'LoginScreen', params: { token: '123456' } })
  ]
})

export class ChangePassword extends Component {
    static navigationOptions = {
        tabBarLabel: '数据',
        tabBarVisible: false
    };
    
    constructor(props) {
      super(props)
      this.state = {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      }
    }
    render() {
        return(
            <ScrollView style={{ backgroundColor: '#fff' }}>
                 <InputItem type="password" placeholder="旧密码" onChange={(value) => {
                  this.state.oldPassword = value
                 }}>旧密码</InputItem>
                 <InputItem type="password" placeholder="新密码" onChange={(value) => {
                  this.state.newPassword = value
                 }}>新密码</InputItem>
                 <InputItem type="password" placeholder="确认密码" onChange={(value) => {
                  this.state.confirmPassword = value
                 }}>确认密码</InputItem>
                        <WhiteSpace />
                            <Button onClick={this.changePassword} type="warning">确认修改</Button>
            </ScrollView>
        );
    }
    changePassword = () => {
      var opts = {
        method: "POST",
        headers:{
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: `oldPassword=${this.state.oldPassword}&newPassword=${this.state.newPassword}`
      }
      // console.log('信心', opts)
      let url = Urls.UPDATEPASSWORD;
      fetch(url,opts)
      .then(response => response.json())
      .then(data => {
          console.log(data);
          if (data.code==0) { // 修改成功
            Toast.success('修改成功，重新登录', 1);
            this.props.navigation.dispatch(loginAction);
          } else { // 修改失败
            Toast.fail('修改失败，请检查输入', 1);
          }
      })
      .catch((error) => {
        console.log(error);
      })
    }
}